<template>
  <div class="file-list" :style="getFlexDirection">
    <el-tooltip
      effect="dark"
      :content="file.name"
      placement="top"
      :disabled="type === 'text'"
      v-for="file in getFileList"
      :key="file.url"
    >
      <div
        class="file-item"
        :class="type"
        :style="{
          width: type === 'text' && direction === 'vertical' ? '100%' : undefined,
          border: type === 'card' && !readonly ? '1px dashed #999999' : 'none'
        }"
        @click.stop="onPreviewFile(file, fileList)"
      >
        <template v-if="type === 'card'">
          <el-image
            v-if="file.type === EnumFileType.IMAGE"
            :src="file.url"
            fit="cover"
            style="width: 100%; height: 100%"
          />
          <div v-else class="card-item-text">
            <i class="el-icon-document" style="font-size: 24px" />
          </div>
          <div v-if="supportPreview" class="priview-btn">预览</div>
          <i
            v-if="!readonly"
            class="el-icon-error close-btn"
            @click.stop="onRemoveFile(file, fileList)"
          />
        </template>
        <template v-else>
          <span
            :style="{
              'flex-grow': direction === 'vertical' ? 1 : 0,
              width: direction === 'vertical' ? '100px' : undefined,
              'margin-right': direction === 'vertical' ? undefined : '8px'
            }"
            >{{ file.name }}</span
          >
          <i
            v-if="!readonly"
            class="el-icon-error close-btn"
            style="margin-left: 6px"
            @click.stop="onRemoveFile(file, fileList)"
          />
        </template>
      </div>
    </el-tooltip>
    <div class="file-item" :class="type" v-if="$slots.default" @click="onAddFile">
      <slot />
    </div>
    <PreviewDialog
      v-if="showPreview"
      :visible="showPreview"
      :file-list="fileList"
      :defaultIndex="fileIndex"
      :readonly="!supportDownload"
      @close="showPreview = false"
    />
  </div>
</template>

<script>
import { getFileType } from '@/utils';
import PreviewDialog from '@/components/FilePreview/PreviewDialog.vue';

export default {
  name: 'UploadFileList',
  props: {
    fileList: {
      type: Array,
      default: () => []
    },
    type: {
      type: String,
      default: 'text'
    },
    direction: {
      type: String,
      default: 'vertical'
    },
    // 是否支持文件预览
    supportPreview: {
      type: Boolean,
      default: true
    },
    // 是否支持文件下载
    supportDownload: {
      type: Boolean,
      default: true
    },
    // 是否只读，只读不能删除
    readonly: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    getFileList() {
      let temp = this.fileList.map(file => {
        return {
          ...file,
          type: this.getFileType(file),
          error: false
        };
      });
      return temp;
    },
    getFlexDirection() {
      return {
        'flex-direction': this.direction === 'vertical' && this.type !== 'card' ? 'column' : 'row'
      };
    }
  },
  components: {
    PreviewDialog
  },
  data() {
    return {
      showPreview: false,
      fileIndex: 0
    };
  },
  methods: {
    getFileType(file) {
      if (file && file.type) {
        return file.type;
      } else {
        return getFileType(file.name);
      }
    },
    onRemoveFile(file, fileList) {
      const index = fileList.findIndex(item => item.url === file.url);
      fileList.splice(index, 1);
      this.$emit('remove', file, fileList);
    },
    onPreviewFile(file, fileList) {
      if (!this.supportPreview) return;
      if (this.supportPreview) {
        this.fileIndex = fileList.findIndex(item => item.url === file.url);
        this.$nextTick(() => {
          this.showPreview = true;
          this.$emit('preview', file, fileList);
        });
      } else if (this.supportDownload) {
        this.downloadFile(file.url, file.name);
      }
    },
    onAddFile() {
      this.$emit('add');
    }
  }
};
</script>

<style lang="scss" scoped>
@import '@/assets/style/element-variables.scss';

.file-list {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
  align-items: flex-start;
  flex-wrap: wrap;
}
.file-item.card {
  width: 65px;
  height: 65px;
  line-height: 65px;
  border-radius: 4px;
  color: #999999;
  position: relative;
  text-align: center;
  border: 1px dashed #999999;
  margin: 4px 8px 4px 0px;
  cursor: pointer;
}
.file-item.card .file-name {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 22px;
  line-height: 22px;
  text-align: center;
  color: #3d3d3d;
  font-size: 12px;
  padding: 0px 4px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  cursor: pointer;
}
.file-item.card .priview-btn {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 22px;
  line-height: 22px;
  text-align: center;
  background: rgba(0, 0, 0, 0.5);
  color: #fff;
  font-size: 12px;
  display: none;
  cursor: pointer;
}
.file-item.card:hover .priview-btn {
  display: block;
}
.file-item.card .close-btn {
  position: absolute;
  top: -5px;
  right: -7px;
  width: 16px;
  height: 16px;
  color: #222222;
  font-size: 16px;
  cursor: pointer;
  display: none;
}
.file-item.card:hover .close-btn {
  display: block;
}

.file-item.text {
  display: flex;
  align-items: center;
  height: 36px;
  line-height: 36px;
  color: #3d3d3d;
}
.file-item.text span {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  cursor: pointer;
}
.file-item.text .close-btn {
  color: #d8d8d8;
  cursor: pointer;
}
.file-item.text:hover,
.file-item.text:hover .close-btn {
  color: $--color-primary;
}
</style>
